import java.util.ArrayList;
import java.util.List;

public class num47 {
    public List<List<Integer>> permuteUnique(int[] nums) {
        List<List<Integer>> res = new ArrayList<>();
        List<Integer> path = new ArrayList<>();
        int len = nums.length;
        boolean[] b = new boolean[len];
        dfs(nums,len,0,path,res,b);
        return res;
    }

    private void dfs(int[] nums, int len, int depth, List<Integer> path, List<List<Integer>> res, boolean[] b) {
        if (depth == len) {
            if(!res.contains(path)){
            res.add(new ArrayList<>(path));
            return;
            }else {
                return;
            }
        }
        for (int i = 0; i < len; i++) {
            if(!b[i]) {
                path.add(nums[i]);
                b[i] = true;
                dfs(nums,len,depth + 1,path,res,b);
                b[i] = false;
                path.remove(path.size() - 1);
            }
        }
    }
}

